TARGET      := lib$(notdir $(CURDIR)).so
CUR_HEADER  += reader.h
CUR_HEADER  += typedef.h
SOURCE	    := $(wildcard *.c)  
OBJS        := $(patsubst %.c, %.o, $(SOURCE))
HEADER	    := $(wildcard *.h)
TEST	    := app
APP_DIR	    := /home/ywt/share/project/bin
TEST_DIR    := test
UPDATA_DIR  := /home/ywt/share/project/updata/reader

ifndef ARG
CCG	    := arm-linux-gcc
AR	    := arm-linux-ar
AR_FLAGS    := rcs
C_FLAGS     +=  -Wall -Os
C_FLAGS     += -fPIC -shared
endif



all:$(TARGET)
ifeq  ($(TEST_DIR), $(wildcard $(TEST_DIR)))
	$(CCG) -o $(TEST) test/reader_app.c -lpthread -L./ -lreader
endif

$(TARGET):$(SOURCE)$(HEADER)
	@for dir in $(SUBDIRS); do \
	if [ -d $$dir ]; then \
	cd $$dir && echo "$$dir" && \
	if [  -f Makefile ]; then \
		make  all || exit "$$?"; \
	fi; \
		cd ../; \
	fi; \
	done
	$(CCG) $(C_FLAGS) $(SOURCE) -o $@

.PHONY: clean install
clean:
	@for dir in $(SUBDIRS); do \
      if [ -d $$dir ]; then \
          cd $$dir && echo "$$dir" && \
          if [  -f Makefile ]; then \
              make  clean || exit "$$?"; \
          fi; \
          cd ../; \
      fi; \
      done
	$(RM) *.o *.tmp $(TARGET) $(TEST) 

install:
	@if test ! -d $(APP_DIR);then \
		mkdir -p $(APP_DIR); \
		echo "mkdir $(APP_DIR)"; \
		cp $(TEST) $(VER_TARGET) $(APP_DIR); \
		echo "cp $(TEST) $(VER_TARGET) $(APP_DIR)"; \
	else \
		cp $(TEST) $(VER_TARGET) $(APP_DIR); \
		echo "cp $(TEST)  $(VER_TARGET) $(APP_DIR)"; \
	fi
	@if test ! -d $(UPDATA_DIR);then \
		mkdir -p $(UPDATA_DIR); \
		echo "mkdir $(UPDATA_DIR)"; \
		cp $(CUR_HEADER) $(TARGET) $(UPDATA_DIR);\
		echo "cp $(CUR_HEADER) $(TARGET) $(UPDATA_DIR)";\
	else \
		cp  $(CUR_HEADER) $(TARGET) $(UPDATA_DIR);\
		echo "cp $(CUR_HEADER) $(TARGET) $(UPDATA_DIR)";\
	fi


